package cn.com.fuhuitong.main.home.ui.activity

import androidx.lifecycle.Observer
import cn.com.fuhuitong.app.AppConstance
import cn.com.fuhuitong.aroute.ARouteConstance
import cn.com.fuhuitong.http.HttpResponseStatus
import cn.com.fuhuitong.main.base.ViewModeActivity
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.vlayout.layout.LinearLayoutHelper
import cn.com.fuhuitong.R
import cn.com.fuhuitong.entity.MainBean
import cn.com.fuhuitong.entity.WebConfig
import cn.com.fuhuitong.main.ItemViewType
import cn.com.fuhuitong.main.home.adapter.BulletinListAdapter
import cn.com.fuhuitong.main.home.vm.BulletinViewModel
import com.alibaba.android.arouter.launcher.ARouter
import kotlinx.android.synthetic.main.activity_home_bulletin.*

@Route(path = ARouteConstance.ACTIVITY_URL_HOME_BULLETIN)
class HomeBulletinActivity : ViewModeActivity<BulletinViewModel>() {


    private lateinit var bulletinListAdapter: BulletinListAdapter

    override fun viewModel(): BulletinViewModel = createViewModel()

    override val layoutResId: Int
        get() = R.layout.activity_home_bulletin

    override fun initView() {

        image_bulletin_back.setOnClickListener {
            this.finish()
        }

        this.initViewRecyclerAdapter(
            recycler_bulletin_list, arrayOf(ItemViewType.BULLETIN_LIST)
        )

        delegateAdapter.addAdapter(
            BulletinListAdapter(
                applicationContext, LinearLayoutHelper().apply {
                    setDividerHeight(resources.getDimensionPixelSize(R.dimen.dp_10))
                }
            ).apply {
                bulletinListAdapter = this
            }
        )

        bulletinListAdapter.setOnItemClickListener { view, i, bulletinListEntity ->
            ARouter.getInstance().build(ARouteConstance.ACTIVITY_URL_WEB_VIEW)
                .withSerializable(
                    AppConstance.BUNDLE_WEB_VIEW,
                    MainBean(
                        type = WebConfig.UNKNOWN.apply { desc = bulletinListEntity.title },
                        data = bulletinListEntity.content
                    )
                )
                .navigation()
        }

    }

    override fun initViewMode() {
        viewModel.bulletinListLiveData.observe(this, Observer { response ->
            if (response.status != HttpResponseStatus.Status.SUCCESS)
                return@Observer
            response.response?.data?.let {
                bulletinListAdapter.setAddDataNew(it)
            }
        })
    }

    override fun initData() {
        viewModel.expressNewsList()
    }

}